package com.bl.weilan.service.biz.companyreport.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.CompanyReportEntity;
import com.bl.weilan.dao.repository.CompanyReportRepository;
import com.bl.weilan.service.biz.companyreport.CompanyReportService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.companyreport.CompanyReportAddVo;
import com.bl.weilan.service.vo.companyreport.CompanyReportListVo;
import com.bl.weilan.service.vo.companyreport.CompanyReportModVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class CompanyReportServiceImpl implements CompanyReportService {
    @Autowired
    CompanyReportRepository companyreportRepository;

    @Override
    public CompanyReportEntity getCompanyReportById(String companyreportId) {
        CompanyReportEntity companyreportEntity = companyreportRepository.findOne(companyreportId);
        return companyreportEntity;
    }

    @Override
    public CompanyReportListVo getCompanyReportLists(String companyId, Pageable pageable ){
        Page<CompanyReportEntity> companyreportEntities;
        CompanyReportListVo companyreportListVo = new CompanyReportListVo();

        companyreportEntities = companyreportRepository.findByCompanyId(companyId,pageable);


        companyreportListVo.setTotalCount(companyreportEntities.getTotalElements());
        companyreportListVo.setList(companyreportEntities.getContent());

        return companyreportListVo;
    }

    @Override
    public void addCompanyReport(CompanyReportAddVo companyreportAddVo) {
        CompanyReportEntity companyreportEntity = new CompanyReportEntity();
        companyreportEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(companyreportAddVo, companyreportEntity);
        companyreportRepository.save(companyreportEntity);

    }

    @Override
    public void del(DelBody id) {
        companyreportRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            companyreportRepository.delete(id);
        }
    }

    @Override
    public void modify(CompanyReportModVo companyreportModVo){
        CompanyReportEntity companyreportEntity = companyreportRepository.findOne(companyreportModVo.getId());

        if (companyreportEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        BeanUtils.copyProperties(companyreportModVo, companyreportEntity);
        companyreportRepository.save(companyreportEntity);
    }
}
